package com.realbrand.net.realbrandpay.ui.activity.mall;

import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.bean.AdressBean;
import com.realbrand.net.realbrandpay.bean.BrandBean;
import com.realbrand.net.realbrandpay.bean.CommodityBean;
import com.realbrand.net.realbrandpay.bean.MollCarBean;
import com.realbrand.net.realbrandpay.inter.IDelCommodityInfo;
import com.realbrand.net.realbrandpay.inter.ISelectAddressInfo;
import com.realbrand.net.realbrandpay.inter.ISelectCommodityInfo;
import com.realbrand.net.realbrandpay.inter.ISettlement;
import com.realbrand.net.realbrandpay.inter.IUpdateDealSum;
import com.realbrand.net.realbrandpay.ui.activity.wallet.personer.adress.IModelManagerAdress;
import com.realbrand.net.realbrandpay.untils.HttpRequest;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/5/14.
 */

public class ModelMollCar {
    /**
     * 获取线上商城购物车的商品数据
     *
     * @param param
     * @param iModelManagerAdress
     */
    public static void getMollCarData(String param, final IModelManagerAdress<List<MollCarBean>> iModelManagerAdress) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, param);
        final List<MollCarBean> data = new ArrayList<MollCarBean>();
        ISelectCommodityInfo IselectCommodityInfo = HttpRequest.request_BASE_URL_VERSION(ISelectCommodityInfo.class, HTTPAPI.BASE_URL);
        IselectCommodityInfo.getMollCarData(body)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MollCarBean>() {
                    @Override
                    public void onCompleted() {
                        iModelManagerAdress.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iModelManagerAdress.onFail(e.toString());
                        L.d(L.TAG, "线上商城返回异常" + e.toString());
                    }

                    @Override
                    public void onNext(MollCarBean mollCarBean) {
                        for (int i = 0; i < mollCarBean.getBrandName().size(); i++) {
                            data.add(mollCarBean);
                        }
                        iModelManagerAdress.onSuccess(data);
                        //List<CommodityBean> commodityBeanList = new ArrayList<CommodityBean>();
//                        if (mollCarBean.getBrandName().size() == 1) {
//
//                        } else {
//                            for (int i = 0; i < mollCarBean.getBrandName().size(); i++) {
//                                if (mollCarBean.getBrandLogo().get(0).equals(mollCarBean.getBrandName().get(i))) {
//
//                                }
//                            }
//                        }

                    }
                });
    }

    /**
     * 获取更新商品数量的结果
     *
     * @param param
     * @param iModelUpdataDeail
     */
    public static void getUpDataDeailResult(String param, final IModelUpdataDeail<String> iModelUpdataDeail) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, param);
        IUpdateDealSum iUpdateDealSum = HttpRequest.request_BASE_URL(IUpdateDealSum.class);
        iUpdateDealSum.upDataDealSum(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String updataDeailResult = response.body().string();
                    L.d(L.TAG, "更新商品数量返回的数据" + updataDeailResult);
                    JSONObject updataDealResultJson = new JSONObject(updataDeailResult);
                    String returncode = updataDealResultJson.getString("RETURNCODE");
                    iModelUpdataDeail.onSuccess(returncode);
                } catch (Exception e) {
                    e.printStackTrace();
                    iModelUpdataDeail.onErrorr();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                iModelUpdataDeail.onFail(t.toString());
            }
        });
    }

    //删除
    public static void deleteCommodity(String prama, final IModelUpdataDeail<String> UpdataDeail) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, prama);
        IDelCommodityInfo iDelCommodityInfo = HttpRequest.request_BASE_URL(IDelCommodityInfo.class);
        iDelCommodityInfo.deleteCommodity(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String DeteleResult = response.body().string();
                    L.d(L.TAG, "删除商品返回的数据" + DeteleResult);
                    JSONObject DeteleResultJson = new JSONObject(DeteleResult);
                    String returncode = DeteleResultJson.getString("RETURNCODE");
                    UpdataDeail.onSuccess(returncode);
                } catch (Exception e) {
                    e.printStackTrace();
                    UpdataDeail.onErrorr();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    //提交订单
    public static void commitOrder(String prams, final IModelUpdataDeail<String> iCommitOrder) {
        RequestBody body = RequestBody.create(HTTPAPI.JSON, prams);
        ISettlement iSettlement = HttpRequest.request_BASE_URL(ISettlement.class);
        iSettlement.commitOrder(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String commitOrderResult = response.body().string();
                    L.d(L.TAG, "提交订单返回的结果" + commitOrderResult);
                    JSONObject commitOrderJson = new JSONObject(commitOrderResult);
                    String returncode = commitOrderJson.getString("RETURNCODE");
                    iCommitOrder.onSuccess(returncode);
                } catch (Exception e) {
                    e.printStackTrace();
                    L.d(L.TAG, "提交订单返回结果异常" + e.toString());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}
